﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla.Security;
using System.Security.Principal;

namespace Dapi.Authentication
{
  [Serializable]
  public class DapiPrincipal : CslaPrincipal
  {
    public DapiPrincipal()
    {
    }
    protected DapiPrincipal(IIdentity identity) : base(identity)
    {
    }
    
    public static bool Login(string username, string password)
    {
      var identity = DapiIdentity.GetIdentity(username, password);
      return SetPrincipal(identity);
    }
    public static bool Load(string username)
    {
      var identity = DapiIdentity.GetIdentity(username);
      return SetPrincipal(identity);
    }
    public static void Logout()
    {
      Csla.ApplicationContext.User = new UnauthenticatedPrincipal();
    }

    private static bool SetPrincipal(IIdentity identity)
    {
      if(identity.IsAuthenticated)
      {
        DapiPrincipal principal = new DapiPrincipal(identity);
        Csla.ApplicationContext.User = principal;
      }

      return identity.IsAuthenticated;
    }
    
  }
}
